package javaMultiThreadInAction.chapter01;

/**
 * @author xinya
 * @version 1.0
 * @project javacore
 * @description 1-7 Java代码的执行线程
 * @date 2024/1/9 16:42:35
 */
public class JavaThreadAnywhere {
    public static void main(String[] args) {
        // 获取当前线程
        Thread currentThread = Thread.currentThread();

        // 获取当前线程的线程名称
        String currentThreadName = currentThread.getName();

        System.out.printf("The main method was executed by thread:%s",
                currentThreadName);

        Helper helper = new Helper("Java Thread AnyWhere");
        helper.run();
    }

    static class Helper implements Runnable {
        private final String message;

        public Helper(String message) {
            this.message = message;
        }

        private void doSomething(String message) {
            // 获取当前线程
            Thread currentThread = Thread.currentThread();
            currentThread.setName("sub-thread");

            // 获取当前线程的线程名称
            String currentThreadName = currentThread.getName();

            System.out.printf("The doSomething method was executed by thread:%s",
                    currentThreadName);
            System.out.println("Do something with " + message);
        }

        @Override
        public void run() {
            doSomething(message);
        }
    }
}
